/*

Copyright (C) 2007 Thomas Geiger (tom.geiger@gmail.com)

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

*/

#include <octave/oct.h>

#include "globuscpp/GridFTPClient.h"

using namespace std;
using namespace globus_common;
using namespace globus_io;

// have a global instance for all functions
static GridFTPClient client;

DEFUN_DLD(gt_sizeof, 
          args, 
          , 
          "Checks the size (kb) of a file (on a gridFTP location). Returns -1 on error."
          ) 
{	
	static const string msg = "Usage: gt_sizeof('gsiftp://host/path/file')";
	
	int nargin = args.length();	
	if (nargin != 1) {
		octave_stdout << msg << endl;
		return octave_value(false);
	}
		
	string filename = args(0).string_value();
	if (error_state) {
		octave_stdout << msg << endl;
		return octave_value(false);
	}
	
	URL* url = URL::Create(filename);
	if (url != NULL) {
		return octave_value( client.size(url) );
	}
	return octave_value(false);	
}

DEFUN_DLD(gt_exists, 
          args, 
          , 
          "Checks whether a file/directory exists (on a gridFTP location). Returns 0 when file does not exists, -1 on error."
          ) 
{	
	static const string msg = "Usage: gt_exists('gsiftp://host/path/filetocheck')";
	
	int nargin = args.length();	
	if (nargin != 1) {
		octave_stdout << msg << endl;
		return octave_value(false);
	}
		
	string filename = args(0).string_value();
	if (error_state) {
		octave_stdout << msg << endl;
		return octave_value(false);
	}
	
	URL* url = URL::Create(filename);
	if (url != NULL) {
		return octave_value( client.exists(url) );
	}
	return octave_value(false);	
}

DEFUN_DLD(gt_delete, 
          args, 
          , 
          "Deletes a file (on a gridFTP location). Returns 0 on success, -1 on error."
          ) 
{	
	static const string msg = "Usage: gt_delete('gsiftp://host/path/filetodelete')";
	
	int nargin = args.length();	
	if (nargin != 1) {
		octave_stdout << msg << endl;
		return octave_value(false);
	}
		
	string filename = args(0).string_value();
	if (error_state) {
		octave_stdout << msg << endl;
		return octave_value(false);
	}
	
	URL* url = URL::Create(filename);
	if (url != NULL) {
		return octave_value( client.del(url) );
	}
	return octave_value(false);	
}

DEFUN_DLD(gt_mkdir, 
          args, 
          , 
          "Creates a directory. Returns 0 on success, -1 on error."
          ) 
{	
	static const string msg = "Usage: gt_mkdir('gsiftp://host/path/directorytocreate')";
	
	int nargin = args.length();	
	if (nargin != 1) {
		octave_stdout << msg << endl;
		return octave_value(false);
	}
		
	string filename = args(0).string_value();
	if (error_state) {
		octave_stdout << msg << endl;
		return octave_value(false);
	}
	
	URL* url = URL::Create(filename);
	if (url != NULL) {
		return octave_value( client.mkdir(url) );
	}
	return octave_value(false);	
}

DEFUN_DLD(gt_rmdir, 
          args, 
          , 
          "Deletes a directory. Returns 0 on success, -1 on error."
          ) 
{	
	static const string msg = "Usage: gt_rmdir('gsiftp://host/path/directorytodelete')";
	
	int nargin = args.length();	
	if (nargin != 1) {
		octave_stdout << msg << endl;
		return octave_value(false);
	}
		
	string filename = args(0).string_value();
	if (error_state) {
		octave_stdout << msg << endl;
		return octave_value(false);
	}
	
	URL* url = URL::Create(filename);
	if (url != NULL) {
		return octave_value( client.rmdir(url) );
	}
	return octave_value(false);	
}

DEFUN_DLD(gt_transfer, 
          args, 
          , 
          "Initiates a 3rd party transfer between two GridFTP hosts. Returns 0 on success, -1 on error."
          ) 
{	
	static const string msg = "Usage: gt_transfer('gsiftp://host/path/sourcefile', 'gsiftp://host/path/destfile')";
	
	int nargin = args.length();	
	if (nargin != 2) {
		octave_stdout << msg << endl;
		return octave_value(false);
	}
		
	string source = args(0).string_value();
	string dest = args(1).string_value();
	if (error_state) {
		octave_stdout << msg << endl;
		return octave_value(false);
	}
	
	URL* sourceUrl = URL::Create(source);
	URL* destUrl = URL::Create(dest);
	if (sourceUrl != NULL && destUrl != NULL) {
		return octave_value( client.transfer(sourceUrl, destUrl) );
	}
	return octave_value(false);	
}

/*
DEFUN_DLD(gt_put, 
          args, 
          , 
          "Copy file to a remote GridFTP host. Returns 0 on success, -1 on error."
          ) 
{	
	static const string msg = "Usage: gt_put('localfile, 'gsiftp://host/path/destfile')";
	
	int nargin = args.length();	
	if (nargin != 2) {
		octave_stdout << msg << endl;
		return octave_value(-1);
	}
		
	string source = args(0).string_value();
	string dest = args(1).string_value();
	if (error_state) {
		octave_stdout << msg << endl;
		return octave_value(-1);
	}
	
	GlobusURL* sourceUrl = GlobusURL::Create(source);
	// read contents of source file into stream
	//istream
	
	GlobusURL* destUrl = GlobusURL::Create(dest);
	if (sourceUrl != NULL && destUrl != NULL) {
		return octave_value( client.put(, destUrl) );
	}
	return octave_value(-1);	
}
*/
